package com.wildwolf.project.wildwolf.utils;

import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;

import com.wildwolf.project.wildwolf.R;

/**
 * 处理状态的工具
 * 
 * @author LEON
 */
public final class Status {

	/** 离线状态代码. */
	public static final int CONTACT_STATUS_DISCONNECT = 100;

	/** 长时间离开状态 */
	public static final int CONTACT_STATUS_UNAVAILABLE = 200;

	/** 离开状态代码.. */
	public static final int CONTACT_STATUS_AWAY = 300;

	/** 忙状态代码.*/
	public static final int CONTACT_STATUS_BUSY = 400;

	/**在线状态代码.. */
	public static final int CONTACT_STATUS_AVAILABLE = 500;

	/**M我吧状态代码.*/
	public static final int CONTACT_STATUS_AVAILABLE_FOR_CHAT = 600;

	/**
	 * 
	 */
	private Status() {
	}

/**
 * 根据状态代码返回出席代码
 * @param status
 * @return
 */
	public static Presence.Mode getPresenceModeFromStatus(final int status) {
		Presence.Mode res;
		switch (status) {
		case CONTACT_STATUS_AVAILABLE:
			res = Presence.Mode.available;
			break;
		case CONTACT_STATUS_AVAILABLE_FOR_CHAT:
			res = Presence.Mode.chat;
			break;
		case CONTACT_STATUS_AWAY:
			res = Presence.Mode.away;
			break;
		case CONTACT_STATUS_BUSY:
			res = Presence.Mode.dnd;
			break;
		case CONTACT_STATUS_UNAVAILABLE:
			res = Presence.Mode.xa;
			break;
		default:
			return null;
		}
		return res;
	}

	/**
	 * 根据状态获取状态图标
	 * 
	 * @param status
	 * @return
	 */
	public static int getStatusFromPresence(final Presence presence) {
		int res = Status.CONTACT_STATUS_DISCONNECT;
		if (presence.getType().equals(Presence.Type.unavailable)) {
			res = Status.CONTACT_STATUS_DISCONNECT;
		} else {
			Mode mode = presence.getMode();
			if (mode == null) {
				res = Status.CONTACT_STATUS_AVAILABLE;
			} else {
				switch (mode) {
				case available:
					res = Status.CONTACT_STATUS_AVAILABLE;
					break;
				case away:
					res = Status.CONTACT_STATUS_AWAY;
					break;
				case chat:
					res = Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT;
					break;
				case dnd:
					res = Status.CONTACT_STATUS_BUSY;
					break;
				case xa:
					res = Status.CONTACT_STATUS_UNAVAILABLE;
					break;
				default:
					res = Status.CONTACT_STATUS_DISCONNECT;
					break;
				}
			}
		}
		return res;
	}

	/**
	 * 上线状态
	 * 
	 * @param status
	 * @return
	 */
	public static boolean statusOnline(final int status) {
		return status != Status.CONTACT_STATUS_DISCONNECT;
	}

	/**
	 * 根据状态获取状态图标
	 * 
	 * @param status
	 * @return
	 */
	public static int getIconBarFromStatus(final int status) {
		int icon = R.drawable.wildwolf_status_icon_available;
		switch (status) {
		case Status.CONTACT_STATUS_AVAILABLE:
			icon = R.drawable.wildwolf_status_icon_available;
			break;
		case Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT:
			icon = R.drawable.wildwolf_status_icon_available;
			break;
		case Status.CONTACT_STATUS_AWAY:
			icon = R.drawable.wildwolf_status_icon_away;
			break;
		case Status.CONTACT_STATUS_BUSY:
			icon = R.drawable.wildwolf_status_icon_busy;
			break;
		case Status.CONTACT_STATUS_UNAVAILABLE:
			icon = R.drawable.wildwolf_status_icon_gray;
			break;
		default:
			icon = R.drawable.wildwolf_status_icon_available;
		}
		return icon;
	}
}
